<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    {include file='public/_meta' /}
</head>

<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form action="" method="post" class="layui-form layui-form-pane">
            <input type="hidden" id="rid" value="{$info.id}" name="rid">
            <div class="layui-form-item">
                <label for="name" class="layui-form-label">
                    <span class="x-red">*</span>角色名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="name" name="name" value="{$info.name}" required="" lay-verify="required"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">
                    拥有权限
                </label>
                <table  class="layui-table layui-input-block">
                    <tbody>
                    {volist name="authlist" id="v"}
                    <tr>
                        <td>
                            <input type="checkbox" name="id[]" lay-skin="primary"
                                   {foreach $info.auths as $ak=>$av}
                                   {if $av.id==$v.id}checked{/if}
                                   {/foreach}
                                   value="{$v.id}" lay-filter="father" title="{$v.title}">
                        </td>
                        <td>
                            <div class="layui-input-block">
                                {foreach $v.children as $ck=>$cv}
                                <input name="id[]" lay-skin="primary" type="checkbox"
                                       {foreach $info.auths as $ak=>$av}
                                        {if $av.id==$cv.id}checked{/if}
                                       {/foreach}
                                       value="{$cv.id}" title="{$cv.title}">
                                {/foreach}
                            </div>
                        </td>
                    </tr>
                    {/volist}
                    </tbody>
                </table>
            </div>
            <div class="layui-form-item layui-form-text">
                <label for="desc" class="layui-form-label">
                    描述
                </label>
                <div class="layui-input-block">
                    <textarea placeholder="请输入内容" id="desc" name="desc" class="layui-textarea">{$info.desc}</textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn" lay-submit="" lay-filter="add">增加</button>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form
            ,layer = layui.layer;

        //自定义验证规则
        form.verify({
            name: function(value){
                if(value.length < 1){
                    return '昵称至少得1个字符啊';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function(data){
            //console.log(data)
            //console.log(JSON.stringify(data.field));
            var ids = [];
            // 获取选中的id
            $.each(data.field,function(index,el){
                //console.log(data.field[index])
                ids.push(data.field[index]);
            })

            //console.log(ids.toString())
            //发异步，把数据提交给php
            $.getJSON("{:url('admin/role/editdo')}",{'data':ids.toString()},function(res){
                //console.log(res);
                if(parseInt(res.statusCode)==200){
                    layer.msg(res.message, {
                            icon: 6
                        },
                        function() {
                            //关闭当前frame
                            xadmin.close();
                            // 可以对父窗口进行刷新
                            xadmin.father_reload();
                        }
                    );
                }else{
                    layer.msg(res.message);
                }
            })
            /*layer.alert("增加成功", {icon: 6},function () {
                // 获得frame索引
                var index = parent.layer.getFrameIndex(window.name);
                //关闭当前frame
                parent.layer.close(index);
            });*/
            return false;
        });


        form.on('checkbox(father)', function(data){

            if(data.elem.checked){
                $(data.elem).parent().siblings('td').find('input').prop("checked", true);
                form.render();
            }else{
                $(data.elem).parent().siblings('td').find('input').prop("checked", false);
                form.render();
            }
        });


    });
</script>

</body>

</html>